//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "UpfrontCdsHelper.h"
using namespace Cephei::QL::Termstructures::Credit;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/Credit/CdsHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (Double upfront, Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<UInt32>^ upfrontSettlementDays, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phUpfrontCdsHelper = NULL;
#endif
        QuantLib::Rate _upfront = (QuantLib::Rate)ValueHelper::Convert (upfront);
        QuantLib::Rate _runningSpread = (QuantLib::Rate)ValueHelper::Convert (runningSpread);
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Integer _settlementDays = (QuantLib::Integer)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        QuantLib::DateGeneration::Rule _rule = (QuantLib::DateGeneration::Rule)rule ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        QuantLib::Natural _upfrontSettlementDays = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (upfrontSettlementDays) ? (QuantLib::Natural)ValueHelper::Convert (upfrontSettlementDays->Value) : 0); //4
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        _ppUpfrontCdsHelper = new boost::shared_ptr<QuantLib::UpfrontCdsHelper> (new QuantLib::UpfrontCdsHelper ( _upfront,  _runningSpread,  _tenor,  _settlementDays,  _calendar,  _frequency,  _paymentConvention,  _rule,  _dayCounter,  _recoveryRate,  _discountCurve,  _upfrontSettlementDays,  _settlesAccrual,  _paysAtDefaultTime ));
        SetCdsHelper (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (Cephei::QL::IQuote^ upfront, Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<UInt32>^ upfrontSettlementDays, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
    CQuote^ _Cupfront;
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phUpfrontCdsHelper = NULL;
#endif
        _Cupfront = safe_cast<CQuote^> (upfront);
        _Cupfront->Lock();
        Handle<QuantLib::Quote>& _upfront = static_cast<Handle<QuantLib::Quote>&> (_Cupfront->GetHandle ()); 
        QuantLib::Rate _runningSpread = (QuantLib::Rate)ValueHelper::Convert (runningSpread);
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Integer _settlementDays = (QuantLib::Integer)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        QuantLib::DateGeneration::Rule _rule = (QuantLib::DateGeneration::Rule)rule ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        QuantLib::Natural _upfrontSettlementDays = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (upfrontSettlementDays) ? (QuantLib::Natural)ValueHelper::Convert (upfrontSettlementDays->Value) : 0); //4
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        _ppUpfrontCdsHelper = new boost::shared_ptr<QuantLib::UpfrontCdsHelper> (new QuantLib::UpfrontCdsHelper ( _upfront,  _runningSpread,  _tenor,  _settlementDays,  _calendar,  _frequency,  _paymentConvention,  _rule,  _dayCounter,  _recoveryRate,  _discountCurve,  _upfrontSettlementDays,  _settlesAccrual,  _paysAtDefaultTime ));
        SetCdsHelper (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cupfront != nullptr) _Cupfront->Unlock();
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (boost::shared_ptr<QuantLib::UpfrontCdsHelper>& childNative, Object^ owner) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
#ifdef HANDLE
	_phUpfrontCdsHelper = NULL;
#endif
	_ppUpfrontCdsHelper = &childNative;
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (QuantLib::UpfrontCdsHelper& childNative, Object^ owner) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
#ifdef HANDLE
	_phUpfrontCdsHelper = NULL;
#endif
	_ppUpfrontCdsHelper = new boost::shared_ptr<QuantLib::UpfrontCdsHelper> (&childNative);
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
    _UpfrontCdsHelperOwner = owner;
    _CdsHelperOwner = owner;
}

Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (CUpfrontCdsHelper^ copy) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
#ifdef HANDLE
	_phUpfrontCdsHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUpfrontCdsHelper = new boost::shared_ptr<QuantLib::UpfrontCdsHelper> (copy->GetShared());
        _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
    }
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (System::Type^ t) : CCdsHelper(CUpfrontCdsHelper::typeid)
{
#ifdef HANDLE
	_phUpfrontCdsHelper = NULL;
#endif
	if (!t->IsSubclassOf(CUpfrontCdsHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (QuantLib::Handle<QuantLib::UpfrontCdsHelper>& childNative, Object^ owner)  : CCdsHelper(CUpfrontCdsHelper::typeid)
{
	_phUpfrontCdsHelper = &childNative;
	_ppUpfrontCdsHelper = &static_cast<boost::shared_ptr<QuantLib::UpfrontCdsHelper>>(childNative.currentLink());
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
    _UpfrontCdsHelperOwner = owner;
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (QuantLib::Handle<QuantLib::UpfrontCdsHelper> childNative)  : CCdsHelper(CUpfrontCdsHelper::typeid)
{
	_phUpfrontCdsHelper = &childNative;
	_ppUpfrontCdsHelper = &static_cast<boost::shared_ptr<QuantLib::UpfrontCdsHelper>>(childNative.currentLink());
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::CUpfrontCdsHelper (QuantLib::UpfrontCdsHelper childNative)  : CCdsHelper(CUpfrontCdsHelper::typeid)
{
#ifdef HANDLE
	_phUpfrontCdsHelper = NULL;
#endif
	_ppUpfrontCdsHelper = new boost::shared_ptr<QuantLib::UpfrontCdsHelper> (new QuantLib::UpfrontCdsHelper (childNative));
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppUpfrontCdsHelper));
}
#endif

Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::~CUpfrontCdsHelper ()
{
    if (_ppUpfrontCdsHelper != NULL)
    {
	    delete _ppUpfrontCdsHelper;
        _ppUpfrontCdsHelper = NULL;
    }
}
Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::!CUpfrontCdsHelper ()
{
    if (_ppUpfrontCdsHelper != NULL)
    {
	    delete _ppUpfrontCdsHelper;
    }
}
QuantLib::UpfrontCdsHelper& Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::GetReference ()
{
    if (_ppUpfrontCdsHelper == NULL) throw gcnew NativeNullException ();
	return **_ppUpfrontCdsHelper;
}
boost::shared_ptr<QuantLib::UpfrontCdsHelper>& Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::GetShared ()
{
    if (_ppUpfrontCdsHelper == NULL) throw gcnew NativeNullException ();
	return *_ppUpfrontCdsHelper;
}
QuantLib::UpfrontCdsHelper* Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::GetPointer ()
{
    if (_ppUpfrontCdsHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppUpfrontCdsHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UpfrontCdsHelper>& Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::GetHandle ()
{
	if (_phUpfrontCdsHelper == NULL)
	{
		_phUpfrontCdsHelper = new Handle<QuantLib::UpfrontCdsHelper> (*_ppUpfrontCdsHelper);
	}
	return *_phUpfrontCdsHelper;
}
#endif
bool Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::HasNative () 
{
	return (_ppUpfrontCdsHelper != NULL);
}

Double Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppUpfrontCdsHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Credit::IUpfrontCdsHelper^ Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper::InitializeDates::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppUpfrontCdsHelper)->initializeDates ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Credit::IUpfrontCdsHelper^ Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper_Factory::Create (Double upfront, Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<UInt32>^ upfrontSettlementDays, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime)
{
    return gcnew CUpfrontCdsHelper ( upfront,  runningSpread,  tenor,  settlementDays,  calendar,  frequency,  paymentConvention,  rule,  dayCounter,  recoveryRate,  discountCurve,  upfrontSettlementDays,  settlesAccrual,  paysAtDefaultTime);
}
Cephei::QL::Termstructures::Credit::IUpfrontCdsHelper^ Cephei::QL::Termstructures::Credit::CUpfrontCdsHelper_Factory::Create (Cephei::QL::IQuote^ upfront, Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<UInt32>^ upfrontSettlementDays, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime)
{
    return gcnew CUpfrontCdsHelper ( upfront,  runningSpread,  tenor,  settlementDays,  calendar,  frequency,  paymentConvention,  rule,  dayCounter,  recoveryRate,  discountCurve,  upfrontSettlementDays,  settlesAccrual,  paysAtDefaultTime);
}
